package com.cjdes.recipes4u;

import com.cjdes.recipes4u.dto.Recipe;
import com.cjdes.recipes4u.dto.RecipeStore;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class DisplayRecipe extends Activity {
	
	private String recipeName;
	private RecipeStore store;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_display_recipe);
		Bundle thisbundle = this.getIntent().getExtras();
		recipeName = (String) thisbundle.get("recipeName");
		store = (RecipeStore) thisbundle.get("recipeStore");
		setTitle(recipeName);
		Recipe selectedRecipe = store.getRecipeByName(recipeName);
		buildSelectedRecipe(selectedRecipe);
		getActionBar().setDisplayHomeAsUpEnabled(true);
	}

	private void buildSelectedRecipe(Recipe selectedRecipe) {
		TextView ingredients = (TextView) findViewById(R.id.ingredientsContent);
		TextView method = (TextView) findViewById(R.id.methodContent);
		ingredients.setText(selectedRecipe.ingredientsToString());
		method.setText(selectedRecipe.getMethod());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_display_recipe, menu);
		getMenuInflater().inflate(R.menu.header, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case android.R.id.home :
				NavUtils.navigateUpFromSameTask(this);
				return true;
			case R.id.action_refresh:
				Intent i = new Intent();
                i.setClass(DisplayRecipe.this, MainActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
				return true;
			default: 
				return super.onOptionsItemSelected(item);
			}
		}
	}
	
	


